﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Serious
{
    public class GameObject2
    {
        public Vector2 Pos;
        public Vector2 Vel;

        public Vector2 Gravity;

        public GameSprite Sprite;
        public Single SpriteCurrentIndex;
        public Single SpriteAnimationDilation;
        public Vector2 SpriteScale;
        public Single SpriteAngle;

        public Rectangle2 Bounds
        {
            get
            {
                if (Sprite == null) return new Rectangle2(Pos, Vector2.Zero);

                Rectangle2 rect = (Rectangle2)(Sprite.BoundsRelative - Sprite.OriginRelative);
                rect *= SpriteScale;
                rect = Rectangle2.FromPoints(rect.LowLow, rect.HiHi);
                return rect + Pos;
            }
        }

        public GameObject2(Vector2 pos, Vector2 vel, Vector2 gravity, GameSprite sprite, Single spriteCurrentIndex, Single spriteAnimationDilation, Vector2 spriteScale, Single spriteAngle)
        {
            Pos = pos;
            Vel = vel;
            Gravity = gravity;
            Sprite = sprite;
            SpriteCurrentIndex = spriteCurrentIndex;
            SpriteAnimationDilation = spriteAnimationDilation;
            SpriteScale = spriteScale;
            SpriteAngle = spriteAngle;
        }
        public GameObject2(Vector2 pos, GameSprite sprite, Vector2 vel) :
            this(pos, vel, Vector2.Zero, sprite, 0.0f, 1.0f, Vector2.One, 0.0f)
        {
        }
        public GameObject2(Vector2 pos, GameSprite sprite)
            : this(pos, sprite, Vector2.Zero)
        {
        }
        public static GameObject2 Clone(GameObject2 obj)
        {
            GameObject2 newObj = new GameObject2(obj.Pos, obj.Sprite, obj.Vel);
            newObj.Gravity = obj.Gravity;
            newObj.SpriteCurrentIndex = obj.SpriteCurrentIndex;
            newObj.SpriteAnimationDilation = obj.SpriteAnimationDilation;
            newObj.SpriteScale = obj.SpriteScale;
            return newObj;
        }

        protected void Move(GameTime gameTime)
        {
            Vel += Gravity * (Single)gameTime.ElapsedGameTime.TotalSeconds;
            Pos += Vel * (Single)gameTime.ElapsedGameTime.TotalSeconds;
        }

        public virtual void Update(GameTime gameTime)
        {
            Move(gameTime);
        }

        protected void ApplyCollisionDelta (Vector2 delta)
        {
            Pos += delta;
        }

        public virtual void HandleCollisionResult(Vector2 delta)
        {
            ApplyCollisionDelta(delta);
        }

        protected void RenderSprite (GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (this.Sprite == null) return;

            Sprite.Render(gameTime, spriteBatch, ref SpriteCurrentIndex, SpriteAnimationDilation, Pos, SpriteScale, SpriteAngle);
        }

        public virtual void Render(GameTime gameTime, SpriteBatch spriteBatch)
        {
            RenderSprite(gameTime, spriteBatch);
        }
    }
}
